/*
- Align:
+ 0: Can Giua
+ 1: Can Trai
+ 2: Can Phai
//*/
// Package
package ClassFrame;

// Lib
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

// Class
public class CFont {
    // Var
    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_RIGHT = 1;
    public static final int ALIGN_CENTER = 2;
    public static final int ALIGN_TOP = 3;
    public static final int ALIGN_BOTTOM = 4;
    
    
    private Image ImageFont;
    private String Map1, Map2;
    private byte[] CharWidth;
    private int CharHeight, SpaceWidth, ImageWidth, Pos;

    // Constructor
    public CFont(String FileName, byte[] CharWidth, int Height, int SpaceWidth, String Map) {
        try {
            ImageFont = Image.createImage(FileName);
        } catch (Exception e) {
        }
        this.CharWidth = CharWidth;
        this.CharHeight = Height;
        this.SpaceWidth = SpaceWidth;
        this.Map1 = Map;
        this.ImageWidth = ImageFont.getWidth();
        if (CharWidth.length != Map.length()) {
            System.out.println("Font '" + FileName + "' error!!! " + CharWidth.length + "-" + Map.length());
        }
    }

    // String Width
    public int StringWidth(byte[] s) {
        int Width = 0;
        for (int i = 0; i < s.length; i++) {
            if (s[i] >= 0) {
                Width += CharWidth[s[i]];
            } else {
                Width += SpaceWidth;
            }
        }

        return Width;
    }

    // SubString Width
    public int SubstringWidth(byte[] s, int Start, int Length) {
        int Width = 0;
        int End = Start + Length;
        if (End > s.length) {
            End = s.length;
        }
        for (int i = Start; i < End && s[i] != -3; i++) {
            if (s[i] >= 0) {
                Width += CharWidth[s[i]];
            } else {
                Width += SpaceWidth;
            }
        }
        return Width;
    }

    // String Width
    public int StringWidth(String s) {
        return SubstringWidth(s, 0, s.length());
    }

    // SubString Width
    public int SubstringWidth(String s, int Start, int Length) {
        int Width = 0;
        int End = Start + Length;
        if (End > s.length()) {
            End = s.length();
        }
        for (int i = Start; i < End; i++) {
            char Ch = s.charAt(i);
            if (Ch == '\n') {
                break;
            }
            Width += CharWidth(Ch);
        }

        return Width;
    }

    // Char Height
    public int GetHeight() {
        return CharHeight;
    }

    // Char Width
    public int charsWidth(char[] charArray, int Start, int Length) {
        String str = new String(charArray, Start, Length);
        return StringWidth(str);
    }

    public int CharWidth(char Ch) {
        Pos = CharIndex(Ch);
        if (Pos != -1) {
            return CharWidth[Pos];
        }
        return SpaceWidth;
    }

    // Char Index
    public int CharIndex(char Ch) {
        return Map1.indexOf(Ch);
    }

    // String --> Byte Array
    public byte[] ToByteArray(String s) {
        byte Data[] = new byte[s.length()];
        for (int i = 0; i < Data.length; i++) {
            if (s.charAt(i) == ' ') {
                Data[i] = -2;
            } else if (s.charAt(i) == '\n') {
                Data[i] = -3;
            } else {
                Data[i] = (byte) Map1.indexOf(s.charAt(i));
                if (Data[i] == -1 && Map2 != null) {
                    Data[i] = (byte) Map2.indexOf(s.charAt(i));
                }
            }
        }

        return Data;
    }

    // Render Text Width Underline
    public void RenderUnderline(Graphics g, String s, int x, int y, int Align, int Line) {
        int x1;
        if (Align == 0) {
            x1 = x;
        } else if (Align == 1) {
            x1 = x - StringWidth(s);
        } else {
            x1 = x - (StringWidth(s) >> 1);
        }
        if (Line == 1) {
            g.drawLine(x1, CharHeight + y, x1 + StringWidth(s) - 1, CharHeight + y);
        }
        Render(g, s, x1, y, 0);
        if (Line == 2) {
            g.drawLine(x1, CharHeight / 2 + y, x1 + StringWidth(s) - 1, CharHeight / 2 + y);
        }
    }

    // Render String
    public void Render(Graphics g, String s, int x, int y, int Align) {
        RenderSub(g, s, 0, s.length(), x, y, Align);
    }

    // Render SubString
    public void RenderSub(Graphics g, String s, int Start, int Length, int x, int y, int Align) {
        int x1, y1, Pos, End = Start + Length;
        if (End > s.length()) {
            End = s.length();
        }
        if (Align == 0) {
            x1 = x;
        } else if (Align == 1) {
            x1 = x - SubstringWidth(s, Start, Length);
        } else {
            x1 = x - (SubstringWidth(s, Start, Length) >> 1);
        }
        y1 = y;
        char Ch;
        for (int i = Start; i < End; i++) {
            Ch = s.charAt(i);
            if (Ch == '\n') {
                if (Align == 0) {
                    x1 = x;
                } else if (Align == 1) {
                    x1 = x - SubstringWidth(s, i + 1, Length - i - Start - 1);
                } else {
                    x1 = x - (SubstringWidth(s, i + 1, Length - i - Start - 1) >> 1);
                }
                y1 += CharHeight;
            } else if ((Pos = CharIndex(Ch)) >= 0) {
                g.drawRegion(ImageFont, 0, Pos * CharHeight, ImageWidth, CharHeight, 0, x1, y1, 20);
                x1 += CharWidth[Pos];
            } else {
                x1 += SpaceWidth;
            }
        }
    }

    // Render Char
    public void RenderChar(Graphics g, char Ch, int x, int y, int Align) {
        if ((Pos = CharIndex(Ch)) >= 0) {
            g.drawRegion(ImageFont, 0, Pos * CharHeight, ImageWidth, CharHeight, 0, x, y, 0);
        }
    }

    // Render String
    public void Render(Graphics g, byte[] s, int x, int y, int Align) {
        RenderSub(g, s, 0, s.length, x, y, Align);
    }

    // Render SubString
    public void RenderSub(Graphics g, byte[] s, int Start, int Length, int x, int y, int Align) {
        int x1, y1, End = Start + Length;
        if (End > s.length) {
            End = s.length;
        }
        if (Align == 0) {
            x1 = x;
        } else if (Align == 1) {
            x1 = x - SubstringWidth(s, Start, Length);
        } else {
            x1 = x - (SubstringWidth(s, Start, Length) >> 1);
        }
        y1 = y;
        for (int i = Start; i < End; i++) {
            if (s[i] == -3) {
                if (Align == 0) {
                    x1 = x;
                } else if (Align == 1) {
                    x1 = x - SubstringWidth(s, i + 1, Length - i - Start - 1);
                } else {
                    x1 = x - (SubstringWidth(s, i + 1, Length - i - Start - 1) >> 1);
                }
                y1 += CharHeight;
            } else if (s[i] >= 0) {
                g.drawRegion(ImageFont, 0, s[i] * CharHeight, ImageWidth, CharHeight, 0, x1, y1, 20);
                x1 += CharWidth[s[i]];
            } else {
                x1 += SpaceWidth;
            }
        }
    }
}
